package com.kaoker.system.dao.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.kaoker.system.dao.ISectionDao;
import com.kaoker.system.entity.Section;

public class SectionDao extends HibernateDaoSupport implements ISectionDao {
    private Logger log = LoggerFactory.getLogger(SectionDao.class);
    
	/**
	 * add or update
	 */
    @Override
	public Long saveSection(Section section) {
		this.getHibernateTemplate().saveOrUpdate(section);		
		return section.getId();
	}
 
    @Override
	public void deleteSection(Long sectionId) {
		 Section section = (Section)this.getSession().load(Section.class, sectionId);
		 this.deleteSection(section);
	}

    @Override
	public void deleteSection(Section section) {
		this.getHibernateTemplate().delete(section);
	}

    @Override
	public void deleteAllSection(List<Section> sections) {
		this.getHibernateTemplate().deleteAll(sections);
	}
	
	@Override
	public List<Section> getSubSections(Section parentSection) {
		String hql = "from Section where PARENT_SECTION_ID=:parentId";
		
		log.debug("getSubSection hql:" + hql + "/" + parentSection.getName());		
		List<Section> list = this.getHibernateTemplate().findByNamedParam(hql, "parentId", parentSection.getId());
		
		return list;
	}
    
	@Override
	public Section getSection(Long id) {
		return this.getHibernateTemplate().get(Section.class, id);
	}
}
